using System;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using System.Windows.Forms;

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;

using EntLibContrib.PolicyInjection.Configuration;
using System.ComponentModel;

namespace EntLibContrib.PolicyInjection.Design.Nodes
{
    /// <summary>
    /// A <see cref="ConfigurationNode"/> that represents the configuration
    /// of an <see cref="TransactionScopeCallHandler" /> in the Configuration
    /// Console.
    /// </summary>
    public class TransactionScopeCallHandlerNode : CallHandlerNode
    {
        private bool complete = true;
        private TransactionScopeOption scopeOption;
        private TimeSpan timeout = TransactionManager.DefaultTimeout;
        private IsolationLevel isolationLevel;
        private EnterpriseServicesInteropOption interopOption;

        /// <summary>
        /// Create a new <see cref="TransactionScopeCallHandlerNode"/> with default settings.
        /// </summary>
        public TransactionScopeCallHandlerNode()
            : this(new TransactionScopeCallHandlerData(Resources.TransactionScopeCallHandlerNodeName))
        {
        }

        /// <summary>
        /// Create a new <see cref="TransactionScopeCallHandlerNode"/> with the given settings.
        /// </summary>
        /// <param name="callHandlerData">Configuration information to initialize the node with.</param>
        public TransactionScopeCallHandlerNode(TransactionScopeCallHandlerData callHandlerData)
            : base(callHandlerData)
        {
            complete = callHandlerData.Complete;
            scopeOption = callHandlerData.ScopeOption;
            timeout = callHandlerData.Timeout;
            isolationLevel = callHandlerData.IsolationLevel;
            interopOption = callHandlerData.InteropOption;
        }

        /// <summary>
        /// Gets or Sets whether or not the Transaction will Complete on success.
        /// </summary>
        [SRDescription("CompleteDescription", typeof(Resources))]
        [SRCategory("CategoryGeneral", typeof(Resources))]
        public bool Complete
        {
            get { return complete; }
            set { complete = value; }
        }

        /// <summary>
        /// Gets or Sets the Transaction Scope Option
        /// </summary>
        [SRDescription("ScopeOptionDescription", typeof(Resources))]
        [SRCategory("CategoryGeneral", typeof(Resources))]
        public TransactionScopeOption ScopeOption
        {
            get { return scopeOption; }
            set { scopeOption = value; }
        }

        /// <summary>
        /// Gets or Sets the Timeout
        /// </summary>
        [SRDescription("TimeoutDescription", typeof(Resources))]
        [SRCategory("CategoryGeneral", typeof(Resources))]
        [TypeConverter(typeof(TimeSpanConverter))]
        public TimeSpan Timeout
        {
            get { return timeout; }
            set { timeout = value; }
        }

        /// <summary>
        /// Gets or Sets the Isolation Level
        /// </summary>
        [SRDescription("IsolationLevelDescription", typeof(Resources))]
        [SRCategory("CategoryGeneral", typeof(Resources))]
        public IsolationLevel IsolationLevel
        {
            get { return isolationLevel; }
            set { isolationLevel = value; }
        }

        /// <summary>
        /// Gets or Sets the Timeout
        /// </summary>
        [SRDescription("InteropOptionDescription", typeof(Resources))]
        [SRCategory("CategoryGeneral", typeof(Resources))]
        public EnterpriseServicesInteropOption InteropOption
        {
            get { return interopOption; }
            set { interopOption = value; }
        }

        /// <summary>
        /// Converts the information stored in the node and generate
        /// the corresponding configuration element to store in
        /// an <see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource" />.
        /// </summary>
        /// <returns>Newly created <see cref="TransactionScopeCallHandlerData"/> containing
        /// the configuration data from this node.</returns>
        public override CallHandlerData CreateCallHandlerData()
        {
            TransactionScopeCallHandlerData callHandlerData = new TransactionScopeCallHandlerData(Name);
            callHandlerData.Complete = Complete;
            callHandlerData.ScopeOption = ScopeOption;
            callHandlerData.Timeout = Timeout;
            callHandlerData.IsolationLevel = IsolationLevel;
            callHandlerData.InteropOption = InteropOption;

            return callHandlerData;
        }

    }
}
